/**
 * 功能描述 收费行编辑
 */

define(function (require, exports, module) {
	var EditGrid = require("editgrid");
	var enmu = require("enums");
	var fetchProperties = require("../constant/fetchProperties");

    var instance = {
        initComponent : function (params, widget) {
            var editgrid = new EditGrid({
            	parentNode : ".J-editgrid",
            	model : {
                    id : "memberRegisterEditGrid",
                    onAdd: function(editors) {
                        var idNumber = editors["idNumber"].getValue();
                        var name = editors["name"].getValue();
                        var sex = editors["sex"].getValue();
                        var phone = editors["phone"].getValue();
                        var member = editors["member"].getValue();
                        var travelContract = editors["travelContract"].getValue();
                        var signUpDate = moment().format("YYYY-MM-DD");


                        if (idNumber && name && sex && phone && member && travelContract) {
                            editgrid.add({
                            	idNumber: idNumber,
                            	name: name,
                            	sex: editors["sex"].getData(sex),
                            	phone: phone,
                            	member: editors["member"].getData(member),
                            	travelContract: editors["travelContract"].getData(travelContract),
                            	signUpDate: signUpDate
                            });
                        } else {
                            return false;
                        }
                    },
                    columns : [{
                    	name : "member",
                    	label : "是否是会员",
                    	format: function(value,row){
                         	return value.value || "";
                        },
                    	editor: {
                             type: "select",
                             options:[{
									key:"true",
									value:"是"
								},{
									key:"false",
									value:"否"
							 }],
                             onEdit: function(plugin, index, rowData) {
                                 plugin.setValue(rowData.member.key);
                             },
                             onChange: function(plugin, index, rowData) {
                            	 rowData.member = plugin.getData(plugin.getValue());
                                 editgrid.update(index, rowData);
                             },
		                    onItemSelect : function(data){
								console.log(data);
							}
                         }
                    },{
                        name : "idNumber",

                        label : "身份证号",
                        editor: {
                        	name : "idNumber",
		                    type: "autocomplete",
		                    url:"api/member/search",
							keyField:"pkMember",
							valueField:"personalInfo.idNumber",
							params:function(str){
								return{
									searchProperties:"personalInfo.name",
									fetchProperties:"pkMember,personalInfo.*"
								};
							},
		                    mustMatch: false,
		                    queryParamName: "s",
		                    //代表选项下拉框中显示的值
		                    format:function(data){
								return data.personalInfo.idNumber;
							},
		                    onEdit: function(plugin, rowIndex, rowData) {
		                   		plugin.setValue(rowData.idNumber);
		                    },
		                    onChange: function(plugin, rowIndex, rowData) {
		                        rowData.idNumber = plugin.getValue();
		                        rowData.personalInfo = plugin.getData(plugin.getValue());
		                        editgrid.update(rowIndex, rowData);
		                    },
		                    onItemSelect : function(data){
								console.log(data);
							}
                        }
                    },{
                        name : "name",
                        label : "姓名",
                        editor: {
                            type: "text",
                            onChange: function(editor, rowIndex, rowData) {
                            	rowData.name = plugin.getValue();
                                editgrid.update(rowIndex, rowData);
                            }
                        }
                    },{
                    	name : "sex",
                        label : "性别",
                        format: function(value,row){
                        	return value.value || "";
                        },
                        editor: {
                            type: "select",
                            options:enmu["com.eling.elcms.basedoc.model.Sex"],
                            onEdit: function(plugin, index, rowData) {
                                plugin.setValue(rowData.sex.key);
                            },
                            onChange: function(plugin, index, rowData) {
                            	rowData.sex = plugin.getData(plugin.getValue());
                            	console.log(rowData)
                                editgrid.update(index, rowData);
                            }
                        }
                    },{
                    	name : "phone",
                    	label : "手机号",
                        editor: {
                            type: "text",
                            onChange: function(editor, rowIndex, rowData) {
                            	rowData.phone = plugin.getValue();
                                editgrid.update(rowIndex, rowData);
                            }
                        }
                    },{
                    	 name : "travelContract",
                    	 label : "会员证号",
                    	 format: function(data,row) {
                             return data.member.personalInfo.idNumber+"-"+data.member.personalInfo.name+
                      			"-"+data.productRule.product.name+"-"+data.memberCertificates;
                         },
                    	 editor: {
                             type: "autocomplete",
                             url: "api/signupregister/querycertificates",
                             params:function(str){
                            	return {
                            		"pkServicePoint":widget.get("subnav").getValue("servicePoint"),
                            		"fetchProperties":"pkTravelContract,memberCertificates,version,member.personalInfo.name," +
                            				"member.personalInfo.idNumber,productRule.product.name,productRule.product.pkProduct," +
                            				"productRule.product.version"
                            	};
                             },
                             mustMatch: false,
                             queryParamName: "s",
                             //代表所选的选项的值是哪一个字段的值
                             keyField: "pkTravelContract",
                             //代表选项下拉框中显示的值
                             format: function(data) {
                                 return data.member.personalInfo.idNumber+"-"+data.member.personalInfo.name+
                                 		"-"+data.productRule.product.name+"-"+data.memberCertificates;
                             },
                             onEdit: function(plugin, rowIndex, rowData) {
                            	 var autocomplete =  plugin.element.find("input").data("autocompleter");
								 autocomplete.setExtraParam("pkServicePoint",widget.get("subnav").getValue("servicePoint"));

                            	 plugin.setValue(rowData.pkTravelContract);
                             },
                             onChange: function(plugin, rowIndex, rowData) {
                                 rowData.travelContract = plugin.getData(plugin.getValue());
                                 editgrid.update(rowIndex, rowData);
                             }
                         }
                    },{
                    	name : "signUpDate",
                    	label : "报名日期",
                    	format :"date",
   					 	formatparams:{
   							mode:"YYYY-MM-DD"
   					 	}
                    },{
                    	 name : "operate",
    					 label : "操作",
    					 format:"button",
						 formatparams:[{
							key: "delete",
	                        icon: "icon-remove",
							handler:function(index,data,rowEle){
								editgrid.remove(index);
							}
						 }]
                    }]
                }
            });
            widget.set("editgrid", editgrid);
        }
    };
    module.exports = instance;
});
